package ui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import org.service.UIService;
import org.vo.User;

public class Login {
    private JFrame frame=new JFrame("登录");
    private JTextField id=new JTextField();
    private JPasswordField password=new JPasswordField();
    private JButton but1=new JButton("登录");
    private JButton but2=new JButton("注册");
    private JLabel lab1=new JLabel("ID",JLabel.RIGHT);
	 private JLabel lab2=new JLabel("密码",JLabel.RIGHT);
	public Login() {
		// TODO Auto-generated constructor stub
		frame.setVisible(true);
		frame.setSize(350,200);
		frame.setLayout(null);
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		lab1.setBounds(10,72,50,27);
		id.setBounds(70,72,150,27);
		but1.setBounds(240,70,60,30);
		but2.setBounds(240,110,60,30);
		lab2.setBounds(10, 114, 50, 27);
		password.setBounds(70,114,150,27);
		frame.add(lab1);
		frame.add(lab2);
		frame.add(id);
		frame.add(but1);
		frame.add(password);
		frame.add(but2);
		
		but1.addActionListener(new ActionListener(){         //"登录"按钮监听器

			
			public void actionPerformed(ActionEvent e) {
				User user=new User();
				user.setId(Integer.parseInt(id.getText()));
				user.setPassword(password.getText());
				int flag=-1;
				// TODO Auto-generated method stub
				try {
				flag=new UIService().login(user);
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
                if(flag==-1)
                	JOptionPane.showMessageDialog(null, "该ID不存在！", "警告！",
							JOptionPane.YES_NO_OPTION);
                else if(flag==1)//如果密码匹配，调用用户界面
				{   frame.dispose();
					new UserTable();
					}
				else
					JOptionPane.showMessageDialog(null, "密码错误，请重试!", "警告！",
							JOptionPane.YES_NO_OPTION);
			}
			
		});
	but2.addActionListener(new ActionListener(){     //"注册"按钮监听器

			
			public void actionPerformed(ActionEvent e) {
				
				// TODO Auto-generated method stub
				frame.dispose();
				new Register();          //调用注册界面
			}
			
		});
	}
	public static void main(String []args)
	{  
		new Login();
		
	}
}
